<template>
  <div>
    <el-table :data="rolesList" style="width: 100%" stripe border>
      <!-- 展开列 -->
      <el-table-column type="expand">
        <template slot-scope="scope">
          <!-- 用栅格系统布局展开的页面 -->
          <el-row
            v-for="(item, index) in scope.row.children"
            :key="item.id"
            :class="['bdbottom', index === 0 ? 'bdtop' : '', 'vcenter']"
          >
            <!-- 一级权限 -->
            <el-col :span="5">
              <el-tag closable @close="closeTag(scope.row, item.id)">
                {{ item.authName }}
              </el-tag>
              <i class="el-icon-caret-right"></i>
            </el-col>
            <!-- 二，三级权限 -->
            <el-col :span="19">
              <el-row
                :class="[index2 === 0 ? '' : 'bdtop', 'vcenter']"
                v-for="(item2, index2) in item.children"
                :key="item2.id"
              >
                <!-- 二级权限 -->
                <el-col :span="6">
                  <el-tag
                    closable
                    type="success"
                    @close="closeTag(scope.row, item2.id)"
                  >
                    {{ item2.authName }}
                  </el-tag>
                  <i class="el-icon-caret-right"></i>
                </el-col>
                <!-- 三级权限 -->
                <el-col :span="18">
                  <el-tag
                    type="warning"
                    v-for="item3 in item2.children"
                    :key="item3.id"
                    closable
                    @close="closeTag(scope.row, item3.id)"
                  >
                    {{ item3.authName }}
                  </el-tag>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
      <!-- 序列 -->
      <el-table-column type="index" label="#"></el-table-column>
      <el-table-column
        prop="roleName"
        label="角色名称"
        width="200"
      ></el-table-column>
      <el-table-column
        prop="roleDesc"
        label="角色描述"
        width="200"
      ></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini">
            编辑
          </el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini">
            删除
          </el-button>
          <el-button
            type="warning"
            icon="el-icon-setting"
            size="mini"
            @click="addRights(scope.row)"
          >
            分配权限
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加权限对话框 -->
    <rights-add-dialogo
      ref="rightAddRef"
      :TreeRightsList="TreeRightsList"
      :roleId="roleId"
    />
  </div>
</template>

<script>
import RightsAddDialogo from './RightsAddDialogo.vue'
export default {
  components: { RightsAddDialogo },
  name: 'RolesTable',
  props: {
    rolesList: {
      type: Array,
      default() {
        return []
      },
    },
  },
  data() {
    return {
      TreeRightsList: [],
      roleId: 0,
    }
  },
  methods: {
    //删除权限操作
    async closeTag(role, rightId) {
      const result = await this.$confirm(
        '此操作将永久删除该权限, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch(err => err)
      if (result !== 'confirm') {
        return this.$message.info('取消删除')
      }
      //请求数据
      const { data: res } = await this.$http.delete(
        'roles/' + role.id + '/rights/' + rightId
      )
      if (res.meta.status !== 200) {
        return this.$message.error('删除失败')
      }
      //响应式删除 返回的data, 是当前角色下最新的权限数据
      role.children = res.data
    },

    //添加权限操作
    async addRights(role) {
      this.roleId = role.id
      console.log(this.roleId)
      //获取数据
      const { data: res } = await this.$http.get('rights/tree')
      if (res.meta.status !== 200) {
        return this.$message.error('获取数据失败')
      }
      this.TreeRightsList = res.data

      //获取三级节点
      this.$refs.rightAddRef.getLeafKeys(role, this.$refs.rightAddRef.defKeys)
      //打开对话框
      this.$refs.rightAddRef.rightsDialogoClose = true
    },
  },
}
</script>

<style lang="less" scoped>
.el-table {
  margin-top: 15px;
}
.el-tag {
  margin: 7px;
}
.bdtop {
  border-top: 1px solid #eee;
}
.bdbottom {
  border-bottom: 1px solid #eee;
}
.vcenter {
  display: flex;
  align-items: center;
}
</style>